package cn.com.mr01.lianxi.mr_lianxi_03;

import java.io.IOException;
import java.util.Iterator;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class IntSumReducer extends
		Reducer<Text, IntWritable, IntWritable, NullWritable> {

	IntWritable s = new IntWritable();

	@Override
	protected void reduce(Text key, Iterable<IntWritable> values,
			Context context) throws IOException, InterruptedException {

		// 累加起来
		Iterator<IntWritable> it = values.iterator();
		// 记录和
		int sum = 0;
		// 记录总条数
		int count = 0;
		while (it.hasNext()) {
			sum += it.next().get();
			System.out.println(sum);
			count++;
		}
		s.set(sum / count);
		// 3. 输出
		context.write(s, NullWritable.get());
	}
}
